Page({
  data: {
    userInfo: {
      avatar: "/images/icons/avatar.png",
      nickName: "",
      studentId: ""
    },
    stats: {
      published: 0,
      found: 0,
      lost: 0
    }
  },

  onLoad: function() {
    this.loadUserInfo();
    this.loadStats();
  },

  onShow: function() {
    // 页面显示时刷新数据
    this.loadUserInfo();
    this.loadStats();
  },

  loadUserInfo: function() {
    // 从缓存或服务器获取用户信息
    const userInfo = wx.getStorageSync('userInfo') || {};
    this.setData({
      userInfo: { ...this.data.userInfo, ...userInfo }
    });
  },

  loadStats: function() {
    // 模拟数据统计
    const stats = {
      published: 5,
      found: 3,
      lost: 2
    };
    this.setData({ stats });
  },

  handleLogin: function() {
    wx.showModal({
      title: '登录',
      content: '请选择登录方式',
      confirmText: '微信登录',
      cancelText: '学号登录',
      success: (res) => {
        if (res.confirm) {
          this.wechatLogin();
        } else {
          this.studentLogin();
        }
      }
    });
  },

  wechatLogin: function() {
    wx.showLoading({
      title: '登录中...',
    });

    // 模拟微信登录
    setTimeout(() => {
      const userInfo = {
        avatar: "/images/icons/avatar.png",
        nickName: "微信用户",
        studentId: "2023181051"
      };
      
      wx.setStorageSync('userInfo', userInfo);
      this.setData({ userInfo });
      
      wx.hideLoading();
      wx.showToast({
        title: '登录成功',
        icon: 'success'
      });
    }, 1000);
  },

  studentLogin: function() {
    wx.navigateTo({
      url: '/packageUser/pages/login/login'
    });
  },

  handleLogout: function() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('userInfo');
          this.setData({
            userInfo: {
              avatar: "/images/icons/avatar.png",
              nickName: "",
              studentId: ""
            }
          });
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          });
        }
      }
    });
  },

  navigateToEdit: function() {
    wx.navigateTo({
      url: '/packageUser/pages/edit-profile/edit-profile'
    });
  },

  navigateToMyItems: function() {
    wx.navigateTo({
      url: '/packageUser/pages/my-items/my-items?type=published'
    });
  },

  navigateToFoundItems: function() {
    wx.navigateTo({
      url: '/packageUser/pages/my-items/my-items?type=found'
    });
  },

  navigateToLostItems: function() {
    wx.navigateTo({
      url: '/packageUser/pages/my-items/my-items?type=lost'
    });
  },

  navigateToSettings: function() {
    wx.navigateTo({
      url: '/packageUser/pages/settings/settings'
    });
  },

  navigateToHelp: function() {
    wx.navigateTo({
      url: '/packageUser/pages/help/help'
    });
  },

  navigateToAbout: function() {
    wx.navigateTo({
      url: '/packageUser/pages/about/about'
    });
  }
});